package com.hjxy.codetools.util;

import com.hjxy.codetools.dto.Desk;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class LinuxInfo {
    public Desk getDeskUsage() {
        Desk desk = new Desk();
       // String parmStr="";
        try {
            Runtime rt = Runtime.getRuntime();
            Process p = rt.exec("df -BG /");// df  / 显示根目录挂载点下的磁盘空间 BG 以G为单位显示
            BufferedReader in = null;
            try {
                in = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String str = null;
                String[] strArray = null;
               // int line = 0;
                while ((str = in.readLine()) != null) {
                   // line++;
//                    if (line != 2) {
//                        continue;
//                    }
                    int m = 0;
                    strArray = str.split(" ");
                    for (String para : strArray) {
                        if (para.trim().length() == 0)
                            continue;
                        m++;
                       // ++m;
                         if (para.endsWith("G") || para.endsWith("Gi")) {
                            // 目前的服务器
                            if (m == 2) {
                                desk.setTotal(para);
                            }
                            if (m == 3) {
                                desk.setUsed(para);
                            }
                        if (m == 4) {
                            desk.setAvailable(para);
                        }
                        }
                        if (para.endsWith("%")) {
                            if (m == 5) {
                                desk.setUse_rate(para);
                            }
                        }

                    }
                   // parmStr+=str;

                }
              //  desk.setParm(parmStr);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return desk;
    }
}
